package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.query.ChartQueryObject;
import cn.wolfcode.wms.query.SaleChartQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IChartService;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartController {
	@Autowired
	private IChartService service;
	@Autowired
	private ISupplierService supplierService;
	@Autowired
	private IBrandService brandService;
	@Autowired
	private IClientService clientService;

	@RequestMapping("/order")
	@RequiredPermission("订货报表")
	public String list(Model model,@ModelAttribute("qo")ChartQueryObject qo){
		model.addAttribute("suppliers", supplierService.selectAll());
		model.addAttribute("brands", brandService.selectAll());
		model.addAttribute("groupByTypes", ChartQueryObject.groupByTypes);
		model.addAttribute("charts", service.orderChart(qo));
		return "chart/order";
	}


	@RequestMapping("/sale")
	@RequiredPermission("订货报表")
	public String sale(Model model,@ModelAttribute("qo")SaleChartQueryObject qo){
		model.addAttribute("clients", clientService.selectAll());
		model.addAttribute("brands", brandService.selectAll());
		model.addAttribute("groupByTypes", SaleChartQueryObject.groupByTypes);
		model.addAttribute("charts", service.saleChart(qo));
		return "chart/sale";
	}

	@RequestMapping("/saleBar")
	public String saleBar(Model model,@ModelAttribute("qo")SaleChartQueryObject qo){
		model.addAttribute("groupByName",SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		List<Map<String, Object>> maps = service.saleChart(qo);
		List<Object> totalAmounts = new ArrayList<>();
		List<Object> groupByNames = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			totalAmounts.add(map.get("totalAmount"));
			groupByNames.add(map.get("groupByName"));
		}
		model.addAttribute("totalAmounts",JSON.toJSONString(totalAmounts));
		model.addAttribute("groupByNames", JSON.toJSONString(groupByNames));
        return "chart/saleBar";
	}
	@RequestMapping("/salePie")
	public String salePie(Model model,@ModelAttribute("qo")SaleChartQueryObject qo){
		model.addAttribute("groupByName",SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		List<Map<String, Object>> maps = service.saleChart(qo);
		List<Map<Object, Object>> totalAmounts = new ArrayList<>();
		List<Object> groupByNames = new ArrayList<>();
		for (Map<String, Object> map : maps) {
            Map<Object, Object> totalAmount = new HashMap<>();
            totalAmount.put("value",map.get("totalAmount"));
            totalAmount.put("name",map.get("groupByName"));
            totalAmounts.add(totalAmount);
			groupByNames.add(map.get("groupByName"));
		}
		model.addAttribute("totalAmounts",JSON.toJSONString(totalAmounts));
		model.addAttribute("groupByNames", JSON.toJSONString(groupByNames));
        return "chart/salePie";
	}

}
